<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Topic;
use App\Select;
use App\User;
use Illuminate\Support\Facades\Auth;

class CreateController extends Controller
{
    //文字投票
    public function createTextVote(){
    	return view('createTextVote');
    }

    //图片投票
    public function createImgVote(){
        return view('createImgVote');
    }

    //文字投票处理
    public function addTextVote(Request $req){
        $inp = $req->input();
        $selects = [];
        foreach ($inp as $k => $v) {

            if (strpos($k, 'titleName') !== false) {
                $sel = [];
                //$path = date('Y-m-d');
                //$name = str_random(10);
                $sel['link'] = $req->input($k);//遍历选项
                $sel['pic'] = '';
                $selects[] =$sel;
                //$req->file('pic'.$i)->move(public_path()."/".$path,$name.'.jpg');//图片文件
                //$selects[] = $path."/".$name;


            }
        }
        //print_r($selects);
        //exit();


        $info = [
            //'uid' => $req->input('uid'),
            'title' => $req->input('title'),//投票标题
            'uid' => Auth::user()->uid,//用户uid
            'desc' => $req->input('desc'),//标题描述
            'type' => $req->input('type'),//投票类型,1表示单选,2表示多选;
            'is_pic' =>$req->input('is_pic'),//投票类型,0表示文字,1表示图片

            'selects' => $selects,//选项

        ];
        //dd($info);
        (new Topic())->saveTopic($info);
        //$to->saveTopic($info);
        return redirect('/');
    }

    //图片投票处理
    public function addImgVote(Request $req){
        //echo count($req->file());
        //print_r($req->file());
        //echo '<br/>';
        //echo $req->file('pic2');
        //exit();

        //dd($req->input());
        //$date = date('Y-m-d');
        //$name = str_random(10);
        //$j = count($req->file());

        /*for ($i=1;$i<=$j;$i++) {
            //echo 'pic'.$i;
            //echo $i;
            $req->file('pic'.$i)->move(public_path()."/".date('Y-m-d'),str_random(10).'.jpg');
        }*/

        /*$path = date('Y-m-d');
        $name = str_random(10);
        $req->file('pic1')->move(public_path()."/".$path,$name.'.jpg');*/

        //exit();

        $inp = $req->input();
        $selects = [];
        $i = 1;
        foreach ($inp as $k => $v) {

            if (strpos($k, 'titleName') !== false) {

                $sel = [];
                $path = '/pic/'.date('Y-m-d');
                $name = str_random(10);
                $sel['link'] = $req->input($k);//遍历选项
                $req->file('pic'.$i)->move(public_path().$path,$name.'.jpg');//图片文件
                $sel['pic'] = $path."/".$name.'.jpg';
                $selects[] = $sel;
                $i+=1;
            }
        }
        //print_r($selects);
        //exit();


        $info = [
            //'uid' => $req->input('uid'),
            'title' => $req->input('title'),//投票标题
            'uid' => Auth::user()->uid,//用户uid
            'desc' => $req->input('desc'),//标题描述
            'type' => $req->input('type'),//投票类型,1表示单选,2表示多选;
            'is_pic' =>$req->input('is_pic'),//投票类型,0表示文字,1表示图片

            'selects' => $selects,//选项

        ];
        //dd($info);
        (new Topic())->saveTopic($info);
        //$to->saveTopic($info);
        return redirect('/');



    	/*$topic['title'] = $req->input('title');//投票标题
    	//$row['uid'] = Auth::user()->uid;
        $topic['desc'] = $req->input('desc');//标题描述
        $topic['type'] = $req->input('type');//投票类型,1表示单选,2表示多选
        $topic['pub_time'] = time();//投票时间
        Topic::insert($topic);//保存到topics表
    	//dd($req->input());


        $tid = Topic::where('title',$req->input('title'))->first();
    	$select['tid'] = $tid->tid;//获取标题tid

        $inp = $req->input();
        foreach ($inp as $k => $v) {
            if (strpos($k, 'titleName') !== false) {
                $sid = substr($k,9);
                $select['sid'] =$sid;
                $select['link'] = $req->input($k);
                Select::insert($select);
            }
        }
        echo "完美";*/
    }

}
